#!/usr/bin/env python

# encoding: utf-8

'''
 * Create File email_auto_register
 * Created by leixu on 2017/12/15
 * IDE PyCharm
'''
from luobocrawler.selenium_script.crawler import Crawler
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import logging
import string
# import logstash
import sys
import json
import asyncio
import random

host = '192.168.2.24'

_LOGGER = logging.getLogger('python-logstash-logger')
_LOGGER.setLevel(logging.INFO)

# _LOGGER.addHandler(logstash.TCPLogstashHandler(host, 5499, version=1))

from luobocrawler.third_imp.phone import phone


class EmailAutoRegisterCrawler(Crawler):
    def __init__(self):
        super().__init__()

    def init_env(self):
        # 指定selenium driver 的类型，默认为chrome
        self.driver_type = "chrome"
        # selenium drvier config,默认采用默认配置
        self.driver_config = custom_chrome_config(is_headless=False, proxy="192.168.2.24:20101")
        # 默认配置配置的简单配置项目
        self.ref_config = {}

    async def rule_1(self):
        driver = self.driver

        async def _register_163(username, phone, password):
            pass

        async def _register_sina(username, phone, password):
            driver.get("https://mail.sina.com.cn/register/regmail.php")
            pass

        async def _register_aliyun(username, phone, password):
            driver.get("https://mailsso.mxhichina.com/aliyun/register?lang=zh_CN")
            await asyncio.sleep(5)

        async def _register_126mail(username, phone, password):
            driver.get("http://reg.email.163.com/unireg/call.do?cmd=register.entrance&from=126mail")
            await asyncio.sleep(5)

        async def _register_outlook(username, phone, password):
            pass

        phone1 = phone.get_random_phone()
        username = random.choices(string.ascii_lowercase, 8) + random.choices(string.digits, 4)
        password = "!qaz2wsx3edc"
        await _register_aliyun()


def custom_chrome_config(folder=None, is_headless=False, proxy=None):
    """

    :param folder:
        where the user data to put
        default is None
    :param is_headless:
        is use headless mode
    :param http_proxy:
        if use http_proxy
    :return:
        dict config
    """
    config = {}
    options = webdriver.ChromeOptions()
    desired_capabilities = DesiredCapabilities().CHROME.copy()
    options.add_argument('--headless') if is_headless else 1
    options.add_argument('window-size=1920x1080')
    options.add_argument(f"--user-data-dir=.\\{folder}") if folder else 1
    options.add_argument(f"--disable-gpu")
    prefs = {"profile.managed_default_content_settings.images": 2,
             "credentials_enable_service": False,
             "profile.default_content_setting_values.notifications": 2}
    options.add_experimental_option("prefs", prefs)
    options.add_argument(f'--proxy-server=sock5://{proxy}') if proxy else 1
    options.add_argument("--disable-notifications")
    config["chrome_options"] = options
    config["desired_capabilities"] = desired_capabilities
    return config
